// 这里放的是 登录模块 请求的异步action
import http from '@/utils/request'
import { RootAction, RootThunkAction } from '@/types/store'
import { ApiResponse, LoginForm, Token } from '@/types/data'
import { setToken } from '@/utils/token'

//登录的action  异步的action => 异步函数
// export const loginAction: RootThunkAction = async (dispatch) => {
//   const res = http.post('authorizations', values)
//   console.log('res', res)

//   // dispatch({type:xxx, payload:xxx})
// }

export const loginActionCreate = (values: LoginForm) => {
  const loginAction: RootThunkAction = async (dispatch) => {
    const res = await http.post<ApiResponse<Token>>('/authorizations', values)
    // console.log('res', res)
    // console.log('res.data.data', res.data.data)
    // 同步的action => 将数据存入仓库 store => 本地
    dispatch({ type: 'login/login', payload: res.data.data } as RootAction)
    setToken(res.data.data)
  }
  return loginAction
}

// 函数专门用于发送验证码的函数
// export const codeActionCreate = (mobile: string): RootThunkAction => {
//   const codeAction = async () => {
//     const res = await http.get('/sms/codes/' + mobile)
//     console.log('res', res)
//   }
//   return codeAction
// }

export const codeActionCreate = (mobile: string): RootThunkAction => {
  return async () => {
    await http.get('/sms/codes/' + mobile)
  }
}
